package com.ruoyi.supermarket.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supermarket.domain.*;
import com.ruoyi.supermarket.domain.Vo.ShCPay;
import com.ruoyi.supermarket.domain.Vo.ShXPay;
import com.ruoyi.supermarket.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.service.IYspayService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 应收汇总Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-30
 */
@Service
public class YspayServiceImpl implements IYspayService 
{
    @Autowired
    private YspayMapper yspayMapper;

    @Autowired
    private XqyspayMapper xqyspayMapper;

    @Autowired
    private SaleMapper saleMapper;

    @Autowired
    private XqsaleMapper xqsaleMapper;

    @Autowired
    private QkMapper qkMapper;
    /**
     * 查询应收汇总
     * 
     * @param yspayId 应收汇总主键
     * @return 应收汇总
     */
    @Override
    public Yspay selectYspayByYspayId(Long yspayId)
    {
        return yspayMapper.selectYspayByYspayId(yspayId);
    }

    /**
     * 查询应收汇总列表
     * 
     * @param yspay 应收汇总
     * @return 应收汇总
     */
    @Override
    public List<Yspay> selectYspayList(Yspay yspay)
    {
        return yspayMapper.selectYspayList(yspay);
    }

    /**
     * 新增应收汇总
     * 
     * @param yspay 应收汇总
     * @return 结果
     */
    @Override
    public int insertYspay(Yspay yspay)
    {
        yspay.setCreateTime(LocalDateTime.now());
        return yspayMapper.insertYspay(yspay);
    }

    /**
     * 修改应收汇总
     * 
     * @param yspay 应收汇总
     * @return 结果
     */
    @Override
    public int updateYspay(Yspay yspay)
    {
        return yspayMapper.updateYspay(yspay);
    }

    /**
     * 批量删除应收汇总
     * 
     * @param yspayIds 需要删除的应收汇总主键
     * @return 结果
     */
    @Override
    public int deleteYspayByYspayIds(Long[] yspayIds)
    {
        return yspayMapper.deleteYspayByYspayIds(yspayIds);
    }

    /**
     * 删除应收汇总信息
     * 
     * @param yspayId 应收汇总主键
     * @return 结果
     */
    @Override
    public int deleteYspayByYspayId(Long yspayId)
    {
        return yspayMapper.deleteYspayByYspayId(yspayId);
    }

    @Override
    public AjaxResult audit(ShXPay shxPay) {
        Yspay yspay = yspayMapper.selectYspayByYspayId(shxPay.getYspayId());
        yspay.setIsSh(shxPay.getIsSh());
        yspay.setShName(SecurityUtils.getLoginUser().getUsername());
        yspay.setShTime(LocalDateTime.now());
        yspayMapper.updateYspay(yspay);
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult hxPay(Yspay yspay) {
        Yspay y = new Yspay();
        y.setIsHx("1");
        y.setYspayId(yspay.getYspayId());
        y.setInvoice(yspay.getInvoice());
        y.setPayTime(LocalDateTime.now());
        yspayMapper.updateYspay(y);
        Yspay ys = yspayMapper.selectYspayByYspayId(yspay.getYspayId());
        Qk qk = qkMapper.selectBySupplyId(ys.getSupplyId());
        qk.setQkPrice(qk.getQkPrice().subtract(ys.getZprice()));
        qkMapper.update(qk);
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult fzd(Yspay yspay) {
        Xqyspay xqyspay = new Xqyspay();
        xqyspay.setYspayId(yspay.getYspayId());
        List<Xqyspay> list = xqyspayMapper.selectXqyspayList(xqyspay);
        for (Xqyspay x : list) {
            Xqsale xqsale = xqsaleMapper.selectXqsaleByXqsaleId(x.getpId());
            Sale sale = saleMapper.selectSaleBySaleId(xqsale.getSaleId());
            sale.setIsZd("0");
            saleMapper.updateSale(sale);
        }
        yspayMapper.deleteYspayByYspayId(yspay.getYspayId());
        xqyspayMapper.deleteXqyspayByYspayId(yspay.getYspayId());
        return AjaxResult.success();
    }

    @Override
    public List<Yspay> selectYspayListFzd(Yspay yspay) {
        List<String> listSh = new ArrayList<String>();
        listSh.add("1");
        listSh.add("2");
        yspay.setIsShList(listSh);
        return yspayMapper.selectYspayListFzd(yspay);
    }

    @Override
    public AjaxResult fsh(Long yspayId) {
        Yspay y = new Yspay();
        y.setYspayId(yspayId);
        y.setIsSh("1");
        yspayMapper.updateYspay(y);
        return AjaxResult.success();
    }
}
